
package com.ruoyi.client.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.BatchIdsForm;
import com.ruoyi.client.pojo.form.CartAddForm;
import com.ruoyi.client.pojo.form.CartUpdateForm;
import com.ruoyi.client.pojo.vo.CartVo;
import com.ruoyi.client.service.CartService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Anonymous
@CrossOrigin
@RestController("ClientCartController")
@Api(value = "v1", tags = "05.购物车模块")
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;

    /**
     * 添加购物车（单个）
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "增加商品到购物车", notes = "传参为商品id、数量")
    @ApiImplicitParam(name = "form", value = "增加购物车参数", dataType = "CartAddForm", required = true)
    @PostMapping("/add")
    public R add(@RequestBody @Valid CartAddForm form) {
        return cartService.add(form) ? R.ok() : R.fail();
    }

    /**
     * 删除购物车（单个）
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除单个购物项", notes = "传参为购物项id")
    @ApiImplicitParam(name = "id", value = "购物车项id", dataType = "Long", required = true)
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        return cartService.deleteById(id) ? R.ok() : R.fail();
    }

    /**
     * 删除（批量）
     */
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "删除多个购物车项", notes = "批量删除购物车项信息")
    @ApiImplicitParam(name = "form", value = "批量删除参数", dataType = "BatchIdsForm", required = true)
    @DeleteMapping(value = "/deleteBatch")
    public R deleteBatchByIds(@RequestBody @Valid BatchIdsForm form) {
        return cartService.deleteBatch(form) ?  R.ok() : R.fail();
    }

    /**
     * 删除我的购物车中的全部商品（清空）
     */
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "删除我的购物车中的全部商品", notes = "清空我的购物车中的全部商品")
    @SaCheckLogin
    @DeleteMapping(value = "/deleteAllByMy")
    public R deleteAllByMy() {
        return cartService.deleteAllByMy() ?  R.ok() : R.fail();
    }

    /**
     * 更改（单个）
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改购物项数据", notes = "参数为购物项id（必选）、数量count、是否勾选isChecked")
    @ApiImplicitParam(name = "form", value = "更新购物车参数", dataType = "CartUpdateForm", required = true)
    @PostMapping("/update")
    public R update(@RequestBody @Valid CartUpdateForm form) {
        return cartService.update(form) ? R.ok() : R.fail();
    }

    /**
     * 更改我的购物车为全勾选或全不选 用户全选/全不选
     */
    @ApiOperationSupport(order = 21)
    @ApiOperation(value = "更改我的购物项数据为全勾选或全不选", notes = "参数为是否勾选isChecked")
    @ApiImplicitParam(name = "isChecked", value = "true-全选，false-全不选", dataType = "Boolean", required = true)
    @PostMapping("/updateIsCheckedAllByMy/{isChecked}")
    public R updateIsCheckedAllByMy(@PathVariable Boolean isChecked) {
        return cartService.updateIsCheckedAll(isChecked) ?  R.ok() : R.fail();
    }

    /**
     * 查询我的购物车列表
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询我的购物车列表", notes = "网页移动端不分页，无参数")
    @GetMapping("/listByMy")
    public R<List<CartVo>> listByMy() {
        return R.ok(cartService.listByMy());
    }

    /**
     * 查询我的购物车列表ForIsChecked(已勾选)
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询我的购物车列表(已勾选商品项）", notes = "网页移动端不分页，无参数")
    @GetMapping("/listIsCheckedByMy")
    public R<List<CartVo>> listIsCheckedByMy() {
        return R.ok(cartService.listIsCheckedByMy());
    }
}
